package com.itheima.manager;

import com.itheima.domain.FlashPromotion;
import com.itheima.service.SmsFlashPromotionService;
import com.itheima.util.Result;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.net.URLDecoder;
import java.time.LocalDate;

@Component
public class SmsFlashPromotionManager {

    @DubboReference
    SmsFlashPromotionService smsFlashPromotionService;

    //根据活动名称分页查询
    public Result flashByPage(Integer pageNum, Integer pageSize, String keyword) throws Exception {

        String title = null;
        System.out.println(keyword);
        if (keyword != null) {
            title = URLDecoder.decode(keyword, "utf-8");
        }
        PageBeanVo pageBeanVo = smsFlashPromotionService.flashByPage(pageNum, pageSize, title);

        return new Result(true, 200, "操作成功", pageBeanVo);
    }

    //添加活动
    public Result create(FlashPromotion smsFlashPromotion) {

        if (smsFlashPromotion.getStatus() == 1) {
            smsFlashPromotion.setStartDate(LocalDate.now());
        }
        smsFlashPromotionService.save(smsFlashPromotion);
        return new Result(true, 200, "操作成功", 1);
    }

    //删除活动
    public Result delete(Integer id) {
        smsFlashPromotionService.delete(id);
        return new Result(true, 200, "操作成功", 1);
    }

    //修改上下线状态
    public Result updateStatus(Integer id, Integer status) {

        FlashPromotion smsFlashPromotion = smsFlashPromotionService.selectById(id);
        smsFlashPromotion.setStatus(status);
        smsFlashPromotionService.updateById(smsFlashPromotion);
        return new Result(true, 200, "操作成功", 1);
    }

    //编辑活动
    public Result updateById(Integer id, FlashPromotion smsFlashPromotion) {

        if (smsFlashPromotion.getStatus() == 1) {
            smsFlashPromotion.setStartDate(LocalDate.now());
        }
        smsFlashPromotion.setId(Long.valueOf(id));
        smsFlashPromotionService.updateById(smsFlashPromotion);

        return new Result(true, 200, "操作成功", 1);
    }

    //获取活动详情
    public Result selectById(Integer id) {
        smsFlashPromotionService.selectById(id);
        return null;
    }
}
